package com.shgsh.platform.service;

import com.alibaba.fastjson.JSON;
import com.shgsh.attribute.model.WordData;
import com.shgsh.common.repository.CURDRepository;
import com.shgsh.common.util.DataTable;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by jessy on 2015/8/8.
 */
@Service
public class WordDataService {
    Logger log = LoggerFactory.getLogger(WordDataService.class);
    @Autowired
    CURDRepository curdRepository;

    /**
     * 保存文字来源数据
     * @param wordData
     */
    public void save(WordData wordData){
        if(wordData != null){
            wordData.setSource_id(UUID.randomUUID().toString());
            log.debug("准备保存行为数据 " + wordData.getSource_id());
            String jsonData = wordData.toJson();
            System.out.println("json= " + jsonData);
            ArrayList<byte[]> hsetParams = new ArrayList<>();
            hsetParams.add(DataTable.WORD_TABLE.getBytes());
            hsetParams.add(wordData.getSource_id().getBytes());
            hsetParams.add(jsonData.getBytes());
            curdRepository.hset(hsetParams);
            log.debug("成功保存文字来源数据 " + wordData.getSource_id());
        }
    }

    /**
     * 根据行为ID 获取文字来源数据
     * @param wordID
     * @return
     */
    public WordData get(String wordID){
        if(!StringUtils.isEmpty(wordID)){
            log.debug("获取 文字来源ID为 " + wordID + " 的文字来源数据 .");
            ArrayList<byte[]> hgetParams = new ArrayList<>();
            hgetParams.add(DataTable.WORD_TABLE.getBytes());
            hgetParams.add(wordID.getBytes());
            String result = curdRepository.hget(hgetParams);
            WordData ad = JSON.parseObject(result, WordData.class);
            return ad;
        }
        return null;
    }

    /**
     * 删除指定行为ID的行为数据
     * @param wordID
     */
    public void delete(String wordID){
        if(!StringUtils.isEmpty(wordID)){
            ArrayList<byte[]> hdelParams = new ArrayList<>();
            hdelParams.add(DataTable.WORD_TABLE.getBytes());
            hdelParams.add(wordID.getBytes());
            curdRepository.hdel(hdelParams);
            log.debug("删除 行为ID为 " + wordID + " 的行为数据");
        }
    }

    /**
     * 更新文字来源数据
     * @param wordData
     */
    public void update(WordData wordData){
        WordData ad = get(wordData.getSource_id());
        ad.setEffect(wordData.getEffect());
        ad.setSection(wordData.getSection());
        ad.setDescription(wordData.getDescription());
        ad.setFeature(wordData.getFeature());
        save(ad);
        log.debug("更新 id为 " + wordData.getSource_id() + " 的文字来源数据");
    }

    /**
     * 列出hashmap 指定表名的所有数据
     * @return
     */
    public List<WordData> list(){
        Map<String, String> map = curdRepository.hscanAll(DataTable.WORD_TABLE);
        Set<Map.Entry<String, String>> entrySet = map.entrySet();
        ArrayList<WordData> wordDatas = new ArrayList<>();
        for(Map.Entry<String, String> it : entrySet){
            String wordData = it.getValue();
            wordDatas.add(JSON.parseObject(wordData, WordData.class));
        }
        log.debug("获取表 " + DataTable.WORD_TABLE + " 的所有数据。");
        return wordDatas;
    }

    public List<String> listAttributes(){
        Map<String, String> map = curdRepository.hscanAll(DataTable.ATTR_INITIAL_DATA);
        Set<Map.Entry<String, String>> entrySet = map.entrySet();
        ArrayList<String> datas = new ArrayList<>();
        for(Map.Entry<String, String> it : entrySet){
            String data = it.getKey();
            datas.add(data);
        }
        System.out.println("datas = " + datas.size());
        log.debug("获取表 " + DataTable.ATTR_INITIAL_DATA + " 的所有key值。");
        return datas;
    }
}
